package com.jyu.hakkatranslation.controller;

import com.jyu.hakkatranslation.Exception.Enums.LoginEnum;
import com.jyu.hakkatranslation.pojo.User;
import com.jyu.hakkatranslation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

@Autowired
private UserService userService;

    /**
     * 登录
     * @param session
     * @return
     */
    @PostMapping("/login")
    public Map login(String username, String password, HttpSession session){

        Map result=new HashMap();
        if("admin".equals(username) && "admin".equals(password)){
            result.put("code",10004);
            result.put("msg","管理员登录");
            return result;
        }
        System.out.println(username);
        System.out.println(password);
        User user = userService.login(username,password);

        if (user!=null) {
            session.setAttribute("user",user);

            result.put("code",LoginEnum.LOGIN_SUCCESS.getState());
            result.put("msg",LoginEnum.LOGIN_SUCCESS.getMessage());
            return result;
        }
        else {
            result.put("code",LoginEnum.PASSWORD_ERROR.getState());
            result.put("msg",LoginEnum.PASSWORD_ERROR.getMessage());
            return result;
        }
    }


    /**
     *注册
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/register")
    public Map register(String username,String password,String email){
    HashMap map = new HashMap();
    int user = userService.queryByUsername(username);
    System.out.println(username);
    System.out.println(password);
    System.out.println(user);

    if (user==0){
        userService.register(username, password,email);
        map.put("code",LoginEnum.REGISTER_SUCCESS.getState());
        map.put("msg",LoginEnum.REGISTER_SUCCESS.getMessage());
        return map;
    }
    else {
        map.put("code",LoginEnum.REGISTER_ERROR.getState());
        map.put("msg",LoginEnum.REGISTER_ERROR.getMessage());
        return map;
    }
}


    @PostMapping("/forgot")
    public String forgot(){
        return "亲！咱们可以先注册一个";
    }

//    @Autowired
//    UserService userService;
//    /**
//     * 注册操作
//     * @param session
//     * @param user
//     * @return
//     */
//    @RequestMapping("/register")
//    public String Register(HttpSession session,@RequestBody User user){
//        String password = user.getPassword();
//        String passwordEnsure = user.getPasswordEnsure();
//        if(passwordEnsure.equals("")||passwordEnsure!= password){
//            session.setAttribute("msg",LoginEnum.REGISTER_ANSWER_INCONSISTENT);
//            return "register";
//        }
//
//        boolean registerState = userService.register(user);
//        if(registerState){//如果注册失败
//            session.setAttribute("msg",LoginEnum.LOGIN_SUCCESS);
//            return "login";
//        }else{
//            session.setAttribute("msg",LoginEnum.REGISTER_ERROR);
//            return "register";
//        }
//    }
//
//    /**
//     * 登录
//     * @param session
//     * @param username
//     * @param password
//     * @return
//     */
//    @GetMapping("/login")
//    public String login(HttpSession session, @Param("username") String username,@Param("password") String password){
//
//        User user= userService.login(username, password);
//        if(user==null){
//            session.setAttribute("msg",LoginEnum.USER_NO_EXIST);
//            return "loginSuccess";
//        }else{
//            session.setAttribute("user",username);
//            return "loginSuccess";
//        }
//
//
////        String sessionCode = (String)request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
////        System.out.println(" session中获取的验证码:  " + sessionCode);
////        if (code.equalsIgnoreCase(sessionCode)){
////            System.out.println("正确");
////            return "验证码相同,验证通过";
////        }else {
////            System.out.println("错误");
////            return "验证码不同,验证失败";
////        }
//    }


}
